<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Payment extends CI_Controller {

    public $render_data;

    function __construct() {
        parent::__construct();

        $this->load->library('template');
        $this->template->set_template('backend');
        $this->load->library('carabiner');

        $this->load->model('payment_model');
        /*
         * check admin and return session data
         */
        if (!is_loggedin()) {
            redirect($this->router->reverseRoute('backend_login') . '?redirect=' . base64_encode(current_url()));
        }
    }

    function index() {

        $this->template->write_view('side_menu', 'backend/side_menu');
        $this->template->write_view('content', 'backend/payment/payment_list');
        $this->template->render();
    }

    function create() {

        if ($_POST) {
            $this->load->library('form_validation');
            $this->form_validation->set_rules('pay_date', 'Pay Date', 'trim|required');
//            $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[members.member_email]');

            if ($this->form_validation->run() === TRUE) {
                $contact = preg_replace('#[^0-9]#', '', $this->input->post('contact'));
                $create_data = array(
                    'pay_date' => $this->input->post('pay_date'),
                    'application_id' => $this->input->post('application_id'),
                    'is_active' => $this->input->post('is_active'),
                    'pay_bank' => $this->input->post('pay_bank'),
                    'slip_code' => $this->input->post('slip_code')
                );

                $insert_id = $this->payment_model->create($create_data);
                redirect('backend/payment/');
            } else {
                echo validation_errors();
                exit;
            }
        } else {
            $form_data['form_url'] = base_url('backend/payment/create');
            $this->load->helper('form');
            $this->template->write_view('side_menu', 'backend/side_menu');
            $this->template->write_view('content', 'backend/payment/payment_form', $form_data);
            $this->template->render();
            return TRUE;
        }
    }

    function edit($payment_id = 0) {


        if ($_POST) {

            $this->load->library('form_validation');
            $this->form_validation->set_rules('pay_date', 'Pay Date', 'trim|required');

            if ($this->form_validation->run() === TRUE) {

                $edit_data = array(
                    'pay_date' => $this->input->post('pay_date'),
                    'application_id' => $this->input->post('application_id'),
                    'is_active' => $this->input->post('is_active'),
                    'pay_bank' => $this->input->post('pay_bank'),
                    'slip_code' => $this->input->post('slip_code')
                );

                $result = $this->payment_model->edit($payment_id, $edit_data);

                $this->load->model('application_model');
                if (isset($edit_data['is_active']) && $edit_data['is_active'] == 1) {
                    $this->application_model->payment_complete($this->input->post('application_id'));
                } else {
                    $this->application_model->payin($this->input->post('application_id'));
                }

                redirect('backend/payment/edit/' . $payment_id);
            } else {
                echo validation_errors();
                exit;
            }
        } else {

            $form_data = $this->payment_model->get($payment_id);
            $form_data['form_url'] = base_url('backend/payment/edit/' . $payment_id);
            $this->load->helper('form');
            $this->template->write_view('side_menu', 'backend/side_menu');
            $this->template->write_view('content', 'backend/payment/payment_form', $form_data);
            $this->template->render();
        }
    }

    function delete($id) {

        $this->payment_model->delete($id);
        return true;
    }

//
//    /**
//     * Change status 
//     */
//    function status($member_id, $member_status) {
//        $member_id = floor($member_id);
//        $member_status = floor($member_status);
//        if ($member_id == 0 || !isset($member_status)) {
//            redirect($this->router->reverseRoute('backend_member'));
//        }
//
//        $result = $this->backend_member->member_status($member_id, $member_status);
//        if ($result) {
//            redirect($this->router->reverseRoute('backend_member'));
//        }
//    }
//
//    function check_email() {
//        if ($_POST) {
//            $email = $this->input->post('email');
//            if ($this->member_model->check_email($email)) {
//                echo TRUE;
//            } else {
//                echo '{"message":"This Email is already in used."}';
//            }
//            return FALSE;
//        }
//    }
//
//    function check_username() {
//        if ($_POST) {
//            $username = $this->input->post('username');
//            if ($this->member_model->check_username($username)) {
//                echo TRUE;
//            } else {
//                echo '{"message":"This username is already in used."}';
//            }
//            return FALSE;
//        }
//    }

    function active($id, $status, $application_id) {

        $this->payment_model->active_status($id, $status);
        $this->load->model('application_model');
        if ($status == 1) {
            $this->application_model->payin($application_id);
        } else {
            $this->application_model->payment_complete($application_id);
        }
        
        return TRUE;
    }

    function json_data() {


        $this->load->library('Datatables');

        $buttons = '<a class="btn btn-default pull-right tbl-delete-row" href="#" rel="' . base_url('backend/payment/delete') . '/$1" ><i class="icon-remove"></i></a>';
        $buttons .= '<a class="btn btn-default pull-right" href="' . base_url('backend/payment/edit') . '/$1" rel="$1"  ><i class="icon-edit"></i></a>';
        $buttons .= '<a class="btn btn-default pull-right status_$2 tbl_active_btn" href="#" rel="' . base_url('backend/payment/active') . '/$1/$2/$3" ><i data-toggle="tooltip" data-placement="bottom" data-title="Active Product" class="icon-play"></i><i data-toggle="tooltip" data-placement="bottom" data-title="Pause Product" class="icon-pause"></i></a>';

        $this->datatables
                ->select('payment_id,application_id,pay_bank,slip_code,pay_date,is_active')
                ->from('payments')
                ->unset_column('payment_id')
                ->add_column('Tools', $buttons, 'payment_id,is_active,application_id')
                ->unset_column('is_active');

        $data['result'] = $this->datatables->generate();
        echo $data['result'];
    }

}
